package com.lizk.study.netty.day006;

import com.lizk.study.netty.entity.ServerContent;
import com.lizk.study.netty.utils.NettyUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.logging.LoggingHandler;

/**
 * @author lizhikui
 * @date 2019/6/22 9:08
 */
public class ProtobufServer {
    public static void main(String[] args) {
        ServerContent serverContent = NettyUtils.createServerContent(NioServerSocketChannel.class);
        serverContent.getServerBootstrap().handler(new LoggingHandler())
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ProtobufVarint32FrameDecoder())
                                .addLast(new ProtobufDecoder(User.Person.getDefaultInstance()))
                                .addLast(new ProtobufVarint32LengthFieldPrepender())
                                .addLast(new ProtobufEncoder())
                                .addLast(new SimpleChannelInboundHandler<User.Person>() {

                                    @Override
                                    protected void channelRead0(ChannelHandlerContext ctx, User.Person msg) throws Exception {
                                        System.out.println(msg.getName());
                                        System.out.println(msg.getEmail());
                                        System.out.println(msg.getSex());
                                        System.out.println(msg.getAge());

                                        ctx.writeAndFlush(User.Person.newBuilder()
                                                .setName("Hxm")
                                                .setAge(3)
                                                .setEmail("lllll")
                                                .setSex(false)
                                                .build());
                                    }

                                    @Override
                                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                                        ctx.close();
                                    }
                                });

                    }
                });
        try {
            NettyUtils.bind(serverContent,9999);
        } catch (InterruptedException e) {
            NettyUtils.stop(serverContent);
        }

    }
}
